package com.vincent.controller.shop;

import com.vincent.common.enums.PaginationEnum;
import com.vincent.common.utils.ResultMapUtil;
import com.vincent.service.ArticleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Vincent on 2014/12/20.
 */
@Controller
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    /**
     * 跳转到文章分类页面
     * @param id
     * @param pageNow
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    public String articleList(@RequestParam Integer id, Integer pageNow, Integer pageSize, HttpServletRequest request) {
        if (pageNow == null || pageNow <= 0) {
            pageNow = PaginationEnum.PAGENOW.getCode();
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = PaginationEnum.PAGESIZE.getCode();
        }
        Map<String, Object> objectMap = articleService.findListPagination(id, pageNow, pageSize);
        Object successObj = ResultMapUtil.getSuccessObj(objectMap);
        request.setAttribute("data", successObj);
        return "/article/list";
    }
    @RequestMapping("/content")
    public String content(@RequestParam Integer id,HttpServletRequest request){
        String ip = request.getRemoteAddr();
        Map<String, Object> objectMap = articleService.articleContent(id, ip);
        Object successObj = ResultMapUtil.getSuccessObj(objectMap);
        request.setAttribute("data", successObj);
        return "/article/content";
    }
}
